import { getAllPositions, getAddPositions, deleteById, changeById, getOneUsers } from '@/services/pisitionsService'
export default {
    namespaced: true,
    state: {
        isLoading: false, //加载状态
        positions: [], //显示所有取货数据
        page: 1,
        limit: 5,
        pageCount: 5,
        total: null,
    },
    mutations: { //同步操作
        setIsLoading(state, payload) {
            state.isLoading = payload;
        },
        setPositions(state, payload) {
            state.positions = payload
        },
        setTotal(state, payload) {
            state.total = payload
        }
    },
    actions: { //异步操作
        //查询全部
        async allPositions(context, payload) {
            if (payload) {
                let { page, pageSize } = payload;
                context.commit("setIsLoading", true);
                let resp = await getAllPositions(page, pageSize);
                console.log("allPositions", resp);
                context.commit("setPositions", resp.rows);
                context.commit("setTotal", resp.total);
                context.commit("setIsLoading", false);
            } else {
                context.commit("setIsLoading", true);
                let resp = await getAllPositions(context.state.page, context.state.limit);
                context.commit("setPositions", resp.rows);
                context.commit("setTotal", resp.total);
                context.commit("setIsLoading", false);
            }


        },
        //查询一个
        async getOneUser(context, payload) {
            context.commit("setIsLoading", true);
            let { type, value } = payload;
            console.log("payload", payload);
            let resp = await getOneUsers(type, value); //传入实参
            console.log("resp", resp);
            context.commit("setPositions", resp.rows); //更新数组
            context.commit("setIsLoading", false)
        },
        //新增
        async addPositions(context, payload) {
            context.commit("setIsLoading", true);
            await getAddPositions(payload);
            console.log("addPositions", payload);
            context.commit("setIsLoading", false)
        },
        //删除
        async deletePositions(context, payload) {
            context.commit("setIsLoading", true);
            await deleteById(payload);
            console.log("deletePositions", payload);
            context.commit("setIsLoading", false)
        },
        //修改
        async changePositions(context, payload) {
            context.commit("setIsLoading", true);
            await changeById(payload);
            console.log("changePositions", payload);
            context.commit("setIsLoading", false)
        }
    }

}